package com.yang.spingboot.yixincallback.qiangqiang;

import java.io.*;
import java.util.Scanner;

/**
 * @author yanwei.yang
 * @date 2022/1/20 18:34
 * @description:
 */
public class Test01 {
    public static void main(String[] args) throws IOException {
        Scanner keyboard = new Scanner(System.in);
        String fileName;
        if (args.length > 0) {
            fileName = args[0];
        } else {
            System.out.print("\nEnter input file name: ");
            fileName = keyboard.nextLine().trim();
        }
        BufferedReader reader = new BufferedReader(new FileReader(fileName), 1024);
        if (args.length > 1) {
            fileName = args[1];
        } else {
            System.out.print("\nEnter output file name: ");
            fileName = keyboard.nextLine().trim();
        }
        //Create output file:
        PrintWriter writer = new PrintWriter(new FileWriter(fileName));
        DocumentIndex index = new DocumentIndex();
        String line;
        int lineNum = 0;
        while ((line = reader.readLine()) != null) {
            lineNum++;
            index.operateAllWords(line, lineNum);
        }
        for (IndexEntry entry : index) {
            writer.println(entry);
        }
        // 关闭流
        reader.close();
        writer.close();
        System.out.println("Success.");
        System.out.print("\nOpen the  output file name: y/n? ");
        String y = keyboard.nextLine().trim();
        if (y.equals("y")) {
            String result = null;
            BufferedReader br = new BufferedReader(new FileReader(fileName), 1024);
            while ((result = br.readLine()) != null) {
                System.out.println(result);
                reader.close();
            }
        }
        keyboard.close();
    }


}
